package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmgWarehouseBo;
import com.ruoyi.system.domain.vo.EmgWarehouseVo;
import com.ruoyi.system.domain.EmgWarehouse;
import com.ruoyi.system.mapper.EmgWarehouseMapper;
import com.ruoyi.system.service.IEmgWarehouseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物资仓库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class EmgWarehouseServiceImpl implements IEmgWarehouseService {

    private final EmgWarehouseMapper baseMapper;

    /**
     * 查询物资仓库
     */
    @Override
    public EmgWarehouseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物资仓库列表
     */
    @Override
    public TableDataInfo<EmgWarehouseVo> queryPageList(EmgWarehouseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmgWarehouse> lqw = buildQueryWrapper(bo);
        Page<EmgWarehouseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物资仓库列表
     */
    @Override
    public List<EmgWarehouseVo> queryList(EmgWarehouseBo bo) {
        LambdaQueryWrapper<EmgWarehouse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmgWarehouse> buildQueryWrapper(EmgWarehouseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmgWarehouse> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTypeId()), EmgWarehouse::getTypeId, bo.getTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), EmgWarehouse::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatarUrl()), EmgWarehouse::getAvatarUrl, bo.getAvatarUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), EmgWarehouse::getAreaId, bo.getAreaId());
        lqw.eq(ObjectUtil.isNotNull(bo.getDeptId()), EmgWarehouse::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), EmgWarehouse::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), EmgWarehouse::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EmgWarehouse::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLeader1()), EmgWarehouse::getLeader1, bo.getLeader1());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone1()), EmgWarehouse::getPhone1, bo.getPhone1());
        lqw.eq(StringUtils.isNotBlank(bo.getLeader2()), EmgWarehouse::getLeader2, bo.getLeader2());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone2()), EmgWarehouse::getPhone2, bo.getPhone2());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmgWarehouse::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增物资仓库
     */
    @Override
    public Boolean insertByBo(EmgWarehouseBo bo) {
        EmgWarehouse add = BeanUtil.toBean(bo, EmgWarehouse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物资仓库
     */
    @Override
    public Boolean updateByBo(EmgWarehouseBo bo) {
        EmgWarehouse update = BeanUtil.toBean(bo, EmgWarehouse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmgWarehouse entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除物资仓库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
